﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using MTGScore_Dice.Resources;
using Microsoft.Phone.Shell;
using System.Threading;

namespace MTGScore_Dice
{
    public enum DiceType
    {
        Type4,
        Type6,
        Type8,
        Type10,
        Type12,
        Type20,
        Type100
    }
    public partial class MainPage : PhoneApplicationPage
    {

        public static string MTGScore_ID = "5b8e1d81-a866-e011-81d2-78e7d1fa76f8";
        public static string MTGCards_ID = "8e788bb0-9067-e011-81d2-78e7d1fa76f8";
        public static string MTGHelps_ID = "8ee0b8fe-8e67-e011-81d2-78e7d1fa76f8";
        public static string MTGDice_ID = "db4da84e-5768-e011-81d2-78e7d1fa76f8";
        public static string InformaTreni_ID = "75c4de83-134c-e011-854c-00237de2db9e";
        public static string ChainingExplosion_ID = "6f6e33a9-6558-e011-854c-00237de2db9e";
        public static string EbookReader_ID = "c7ded5b0-17d6-df11-a844-00237de2db9e";
        public static string TapSequence_ID = "3cf38972-3573-e011-81d2-78e7d1fa76f8";
        public static string BoardGameSuite_ID = "e8201966-ff70-e011-81d2-78e7d1fa76f8";
        public static string Enigma_ID = "ad6556b4-bf71-e011-81d2-78e7d1fa76f8";
        public static string IdeoPuzzle_ID = "f7ffd022-63dc-df11-a844-00237de2db9e";
        public static string TriangleShooter_ID = "a08a8336-3cfe-df11-9264-00237de2db9e";
        public static string SapceBattle_ID = "0ee14cb1-cad9-df11-a844-00237de2db9e";

        // Constructor
        DiceType diceType1 = DiceType.Type20;
        DiceType diceType2 = DiceType.Type20;
        DiceType diceTypeAll = DiceType.Type20;
        Random random = new Random();
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

            Ads_List.ItemsSource = App.ViewModel.Ads;
            Microsoft.Advertising.Mobile.UI.AdControl.TestMode = false;
            ApplicationBar.Buttons.Clear();
            ApplicationBarIconButton credits = new ApplicationBarIconButton() { Text = Strings.Credits, IsEnabled = true };

            if (App.CurrentTheme != App.Theme.Dark)
                credits.IconUri = new Uri("\\Images\\dark\\appbar.questionmark.rest.png", UriKind.Relative);
            else
                credits.IconUri = new Uri("\\Images\\light\\appbar.questionmark.rest.png", UriKind.Relative);
            credits.Click += OpenCredits_Button_Click;
            ApplicationBar.Buttons.Add(credits);

            try
            {
                var occ = Research.LoadUtilizations() + 1;
                new Thread(new ThreadStart(() =>
                {
                    Research.getHTML(Research.GetUtilizationUrl(occ, App.isTrial()),
                        (s) =>
                        {
                            Research.DeleteUtilizations();
                        },
                        () =>
                        {
                            Research.SaveUtilizations(occ);
                        });
                })).Start();
            }
            catch { }
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
            Dice_All_Click(null, null);
        }

        int GetDiceResult(DiceType type)
        {
            int r = random.Next(10000);
            switch (type)
            {
                case DiceType.Type4:

                    return r % 4 + 1;
                case DiceType.Type6:

                    return r % 6 + 1;
                case DiceType.Type8:

                    return r % 8 + 1;
                case DiceType.Type10:

                    return r % 10;
                case DiceType.Type12:

                    return r % 12 + 1;
                case DiceType.Type20:

                    return r % 20 + 1;
                case DiceType.Type100:

                    return (r % 10) * 10;
            }


            return 0;
        }

        private void Dice_1_Click(object sender, RoutedEventArgs e)
        {
            Result_1.Text = "" + GetDiceResult(diceType1);
        }

        private void Dice_2_Click(object sender, RoutedEventArgs e)
        {
            Result_2.Text = "" + GetDiceResult(diceType2);
        }

        private void Dice_All_Click(object sender, RoutedEventArgs e)
        {
            Result_1.Text = "" + GetDiceResult(diceTypeAll);
            Result_2.Text = "" + GetDiceResult(diceTypeAll);
        }
        private void OpenCredits_Button_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/CreditsPage.xaml", UriKind.Relative));
        }
        private DiceType GetDiceType(object sender, DiceType prev)
        {
            try
            {
                var t = (MenuItem)sender;
                String s = (String)t.Header;

                if (s == GetDiceTypeString(DiceType.Type4))
                    return DiceType.Type4;
                if (s == GetDiceTypeString(DiceType.Type6))
                    return DiceType.Type6;
                if (s == GetDiceTypeString(DiceType.Type8))
                    return DiceType.Type8;
                if (s == GetDiceTypeString(DiceType.Type10))
                    return DiceType.Type10;
                if (s == GetDiceTypeString(DiceType.Type12))
                    return DiceType.Type12;
                if (s == GetDiceTypeString(DiceType.Type20))
                    return DiceType.Type20;
                if (s == GetDiceTypeString(DiceType.Type100))
                    return DiceType.Type100;
                return prev;
            }
            catch (Exception ex)
            {
                ex.ToString();
                return prev;
            }
        }
        string GetDiceTypeString(DiceType type)
        {
            if (type == DiceType.Type4)
                return "4";
            if (type == DiceType.Type6)
                return "6";
            if (type == DiceType.Type8)
                return "8";
            if (type == DiceType.Type10)
                return "10";
            if (type == DiceType.Type12)
                return "12";
            if (type == DiceType.Type20)
                return "20";
            if (type == DiceType.Type100)
                return "100";
            return "6";
        }
        private void Type1_Click(object sender, RoutedEventArgs e)
        {
            diceType1 = GetDiceType(sender, diceType1);
            DiceTypeText1.Text = GetDiceTypeString(diceType1);
        }
        private void Type2_Click(object sender, RoutedEventArgs e)
        {
            diceType2 = GetDiceType(sender, diceType2);
            DiceTypeText2.Text = GetDiceTypeString(diceType2);
        }
        private void TypeAll_Click(object sender, RoutedEventArgs e)
        {
            diceTypeAll = GetDiceType(sender, diceTypeAll);
            DiceTypeTextAll.Text = GetDiceTypeString(diceTypeAll);

            diceType1 = GetDiceType(sender, diceTypeAll);
            DiceTypeText1.Text = GetDiceTypeString(diceType1);

            diceType2 = GetDiceType(sender, diceTypeAll);
            DiceTypeText2.Text = GetDiceTypeString(diceType2);
        }
        private void GestureListener_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            try
            {
                Border border = sender as Border;
                ContextMenu contextMenu = ContextMenuService.GetContextMenu(border);
                if (contextMenu.Parent == null)
                {
                    contextMenu.IsOpen = true;
                }
            }
            catch (Exception)
            { }
        }

        /*
        private void TypeChooserText_2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            GestureListener_Tap(DiceBorder2,null);
        }
        private void TypeChooserText_1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            GestureListener_Tap(DiceBorder1, null);
        }
        private void TypeChooserText_All_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            GestureListener_Tap(DiceBorderAll, null);
        }*/

        private void TapDice1_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            GestureListener_Tap(DiceBorder1, null);
        }
        private void TapDice2_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            GestureListener_Tap(DiceBorder2, null);
        }
        private void TapDiceAll_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            GestureListener_Tap(DiceBorderAll, null);
        }

        private void AdControl_AdControlError(object sender, Microsoft.Advertising.Mobile.UI.ErrorEventArgs e)
        {
            //AdControl1.Visibility = System.Windows.Visibility.Collapsed;
            Dispatcher.BeginInvoke(() =>
            {
                Ads1_Button.Visibility = System.Windows.Visibility.Visible;
            });
        }


        private void Ads_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Ads_List.SelectedItem == null)
                return;
            AdsViewModel ads = Ads_List.SelectedItem as AdsViewModel;
            Ads_List.SelectedItem = null;
            if (ads.Name != Strings.OtherProducts && ads.ID != "")
                OpenMarket(ads.ID);


        }


        private void Ads_Button_Click(object sender, RoutedEventArgs e)
        {
            PivotControl.SelectedItem = AdsPivot_Name;
        }


        private void OpenMarket(string ID)
        {

            MarketplaceDetailTask marketplaceDetailTask = new MarketplaceDetailTask();
            marketplaceDetailTask.ContentIdentifier = ID;
            marketplaceDetailTask.Show();
        }
    }
}